//Railscasts Episode 197
// by: Ryan Bates
// http://railscasts.com/episodes/197-nested-model-form-part-2
function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();
}

//Railscast Episode 197
// by: Ryan Bates
// http://railscasts.com/episodes/197-nested-model-form-part-2
function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).up().up().up().insert({
    before: content.replace(regexp, new_id)
  });
}

//Replaces elements in a form with users selection,
//then hides search results and displays the form.
function show_ec_form(a_name, a_value, a_new) {
  var form = document.getElementById("ec-form");
  var h = $(form).down('input', 1);
  
  //Replace form values with selected artist
  if (a_new == "true") {
    $(h).name = "artist_name"; 
  } else {
    $(h).name = "artist_id";
  }
  $(h).value = a_value;
  $(form).down('label').replace(a_name);
  
  //Hide search results, show form
  document.getElementById("ec-search-artists").hide();  
  form.show();
  
  
}

function show_form(a_name, a_value, a_new) {
  var form = document.getElementById("credit-form");
  var h = $(form).down('input', 1);
  
  //Replace form values with selected artist
  if (a_new == "true") {
    $(h).name = "artist_name"; 
  } else {
    $(h).name = "artist_id";
  }
  $(h).value = a_value;
  $(form).down('label').replace(a_name);
  
  //Hide search results, show form
  document.getElementById("credit-results").hide();  
  form.show();
  
  
}